package mysql

import "gorm.io/gorm"

type Class struct {
	gorm.Model
	UserId int     `gorm:"type:int;comment:管理员id"`
	Name   string  `gorm:"type:varchar(50);comment:课程名称"`
	Price  float64 `gorm:"type:decimal(10,2);comment:课程价格"`
	Cover  string  `gorm:"type:varchar(255);comment:课程封面"`
	Status int     `gorm:"type:int;comment:课程状态:1-上架，2-下架，3-删除"`
	Num    int     `gorm:"type:int;comment:课程库存"`
}

func (Class) TableName() string {
	return "class"
}

func (c *Class) AddClass() error {
	return DB.Create(&c).Error
}

func (c *Class) List() (result []Class, err error) {
	err = DB.Find(&result).Error
	if err != nil {
		return nil, err
	}
	return result, nil
}

func (c *Class) Detail(id int64) (result []Class, err error) {
	err = DB.Where("id=?", id).Find(&result).Error
	if err != nil {
		return nil, err
	}
	return result, nil
}

func (c *Class) UpdatedClassStatus(id int64, status int64) error {
	return DB.Where("id=?", id).Find(&c).Update("status", status).Error
}

func (c *Class) Find(id int64) (result Class, err error) {
	err = DB.Where("id=?", id).Find(&result).Error
	if err != nil {
		return Class{}, err
	}
	return result, nil
}

func (c *Class) Updated(classId int64, num int) error {
	return DB.Where("id=?", classId).Find(&c).Update("num", num).Error
}
